package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IStrategyCommentRepository;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {


    /**
     * spring-data 会自动注入IStrategyCommentRepository实现类
     *
     * IStrategyCommentRepository 接口继承MongoRepository
     * 拥有了 分页,排序,crud基本功能
     *    底层实现 : AOP 操作--动态代理
     */
    @Autowired
    private IStrategyCommentRepository repository;

    //使用较为底层的贾琏欲执事, 实现mongodb操作, 这里用它来实现较为复杂操作
    @Autowired
    private MongoTemplate template;


    @Override
    public void saveOrUpdate(StrategyComment comment) {
        if(!StringUtils.hasLength(comment.getId())){
            //创建
            comment.setCreateTime(new Date());
        }
        repository.save(comment);
    }

    @Override
    public void save(StrategyComment strategyComment) {
        if (strategyComment != null){
            strategyComment.setCreateTime(new Date());
        }
        repository.save(strategyComment);
    }

    @Override
    public Page query(StrategyCommentQuery qo) {
        //mongodb查询对象
        Query query = new Query();

        //查询条件
        if (qo.getDetailId() != null){
            Criteria criteria = Criteria.where("detailId").is(qo.getDetailId());
            query.addCriteria(criteria);
        }
        //查总数
        Long count = template.count(query, StrategyComment.class);
        //若总条数为空,无需再往下执行
        if (count == null){
            return Page.empty();
        }
        //分页对象
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "createTime");
        query.with(pageable);

        //查询当前页的数据
        List<StrategyComment> list = template.find(query, StrategyComment.class);

        return new PageImpl(list,pageable,count);
    }

    @Override
    public void commentThumbUp(String toid, Long fromid) {
        //获取当前的评论
        Optional<StrategyComment> optional = repository.findById(toid);
        StrategyComment comment = optional.get();

        if (comment == null){
            return;
        }
        //点赞用户id集合
        List<Long> thumbuplist = comment.getThumbuplist();
        //判断是点赞还是取消点赞
        if (thumbuplist.contains(fromid)){
            //取消点赞
            comment.setThumbupnum(comment.getThumbupnum() - 1);
            thumbuplist.remove(fromid);
        }else{
            //点赞
            comment.setThumbupnum(comment.getThumbupnum() + 1);
            thumbuplist.add(fromid);
        }
        repository.save(comment);
    }

    @Override
    public StrategyComment getByNewTime(Long id) throws Exception {
        List<StrategyComment> all = repository.findByDetailId(id);
        //System.out.println(all);
        Date now = new Date();
        long mindifftime = 0;
        StrategyComment  strategyComment = new StrategyComment();
        for (StrategyComment base : all) {
            BeanUtils.copyProperties(strategyComment,base);
            mindifftime = base.getCreateTime().getTime();
        }
        //System.out.println(mindifftime);
        //System.out.println(strategyComment.getUsername());
        for (StrategyComment comment : all) {
            //System.out.println(comment.getCreateTime());
            long difftime = now.getTime() - comment.getCreateTime().getTime();
            //System.out.println(difftime);
            if (mindifftime > difftime){
                mindifftime = difftime;
                BeanUtils.copyProperties(strategyComment,comment);
            }
        }
        System.out.println(strategyComment.getCreateTime());
        System.out.println(strategyComment.getUsername());
        return strategyComment;
    }
}
